package com.xoa.service.party;


import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.party.InspectorProblemInfoMapper;
import com.xoa.model.party.InspectorProblemInfo;
import com.xoa.model.users.Users;
import com.xoa.util.ExcelUtil;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 程叶同 on 2018/1/6.
 * 空气重污染 工作表
 */
@Service
public class InspectorProblemInfoService {
    @Autowired
    InspectorProblemInfoMapper inspectorProblemInfoMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    /**
    * @Author 程叶同
    * @Description  添加
    * @Date 13:46 2018/1/6
    */
    public BaseWrapper addProblemInfo(InspectorProblemInfo inspectorProblemInfo){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(inspectorProblemInfo!=null){
            int insert = inspectorProblemInfoMapper.insert(inspectorProblemInfo);
            if(insert>0){
                baseWrapper.setMsg("添加成功");
                baseWrapper.setFlag(true);
            }else{
                baseWrapper.setFlag(false);
                baseWrapper.setMsg("添加失败");
            }
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 删除
    * @Date 13:50 2018/1/6
    */
    public BaseWrapper deleteProblemInfo(HttpServletRequest request, Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        if(id!=null){
            InspectorProblemInfo inspectorProblemInfo = inspectorProblemInfoMapper.selectByPrimaryKey(id);

                int i = inspectorProblemInfoMapper.deleteByPrimaryKey(id);
                if(i>0){
                    baseWrapper.setFlag(true);
                    baseWrapper.setMsg("删除成功");
                }else{
                    baseWrapper.setMsg("删除失败");
                    baseWrapper.setFlag(false);
                }

        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 修改
    * @Date 13:54 2018/1/6
    */

    public BaseWrapper updateProblemInfo(HttpServletRequest request, InspectorProblemInfo inspectorProblemInfo){
        BaseWrapper baseWrapper=new BaseWrapper();
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        if(inspectorProblemInfo!=null){

                int i = inspectorProblemInfoMapper.updateByPrimaryKeySelective(inspectorProblemInfo);
                if(i>0){
                    baseWrapper.setFlag(true);
                    baseWrapper.setMsg("修改成功");
                }else{
                    baseWrapper.setMsg("修改失败");
                    baseWrapper.setFlag(false);
                }

        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 查询全部信息
    * @Date 13:56 2018/1/6
    */
    public BaseWrapper getProblemInfoAll(HttpServletRequest request, Integer page, Integer pageSize, String subcounty, String reportUnitName, String userName){
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        BaseWrapper baseWrapper=new BaseWrapper();
        List<InspectorProblemInfo> resultlists=new ArrayList<InspectorProblemInfo>();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        map.put("reportUnitName",reportUnitName);
        map1.put("reportUnitName",reportUnitName);
        map.put("userName",userName);
        map1.put("userName",userName);
        map.put("subcounty",subcounty);
        map1.put("subcounty",subcounty);
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page",page-1);
        map.put("pageParams",pageParams);
        int total=0;
        resultlists=inspectorProblemInfoMapper.selectList(map);
        total=inspectorProblemInfoMapper.totalItems(map1);
        //获取所属部门
        /*Department department=departmentMapper.getDeptById(user.getDeptId());
        if(department.getDeptParent()==27){
            //属于社区
            map.put("subcounty",subcounty);
            map1.put("subcounty",subcounty);
            resultlists=inspectorProblemInfoMapper.selectList(map);
            total=inspectorProblemInfoMapper.totalItems(map1);

        }else{
            resultlists=inspectorProblemInfoMapper.selectList(map);
            total=inspectorProblemInfoMapper.totalItems(map1);
        }*/
        if(resultlists!=null){
            baseWrapper.setFlag(true);
            baseWrapper.setData(resultlists);
            baseWrapper.setMsg("查询成功");
            baseWrapper.setTotle(total);
        }else{
            baseWrapper.setMsg("查询失败");
            baseWrapper.setFlag(false);
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 查询单条 信息
    * @Date 14:04 2018/1/6
    */
    public BaseWrapper getProblemInfoById(Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(id!=null){
            InspectorProblemInfo inspectorProblemInfo = inspectorProblemInfoMapper.selectByPrimaryKey(id);
            if(inspectorProblemInfo!=null){
                baseWrapper.setData(inspectorProblemInfo);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("查询成功");
            }else{
                baseWrapper.setFlag(false);
                baseWrapper.setMsg("查询失败");
            }
        }
        return  baseWrapper;
    }

    public BaseWrapper outPutFile(HttpServletRequest request, HttpServletResponse response, String ids){
        BaseWrapper baseWrapper=new BaseWrapper();
        List<InspectorProblemInfo> inspectorProblemInfos=null;
        if(ids!=null&&ids!=""){
            String[] id=ids.split(",");
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("id",id);
            inspectorProblemInfos=inspectorProblemInfoMapper.selectList(map);
        }
        try {

            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("空气污染对应工作表", 9);
            String[] secondTitles = {"执行日期","本单位领导带队检查次数", "出动执法检查人员人次", "出动执法检查人员车次", "污染排放企业检查个数","污染排放企业查处个数","污染排放企业检查个数","建筑工地检查个数","建筑工地查处个数","重型柴油机检查车辆数","重型柴油机查处车辆数","露天烧烤查处起数","露天焚烧查处起数","道路扬尘、遗撒查处起数",
                    "道路清洁保洁出动人次","道路清洁保洁出动车次","道路清洁保洁道路条数","道路清洁保洁用水","餐饮企业检查家数","餐饮企业查处家数","其他工作"};
            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

            // String[] beanProperty = {user.getDep().getDeptName(),"userName","userPrivName", "roleAuxiliaryName","online","sex","online","telNoDept","telNoDept","departmentPhone","email"};
            String[] beanProperty = {"commitTime","unitleaderCheck","performLawusernum", "performLawusertrains","pollutionEnterpriseCheck","pollutionEnterpriseSolve","construstionCheck","construstionSolve","construstionArea","dieselengineCheck","dieselengineSolve","outdoorBarbecueSolve","outdoorBurningSolve",
                    "roadDustSolve","roadDustUsers","roadDustTrains","roadDustRoads","roadDustWater","cateringEnterpriseCheck","cateringEnterpriseSolve", "others"};

            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, inspectorProblemInfos, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "空气污染对应工作表.xls";
            filename = FileUtils.encodeDownloadFilename(filename,
                    request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition",
                    "attachment;filename=" + filename);
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return  baseWrapper;
    }

}
